﻿using System;
using System.Linq;

namespace SharePoint.Contrib.Diagnostics
{
    /// <summary>
    /// Inherit from this class to add your own DiagnosticsAreas via the AreasAndCategoriesService
    /// </summary>
    public abstract class Area
    {
        internal event EventHandler<EventArgs<Category>> OnAddingCategory = delegate { };

        /// <summary>
        /// The name of the area
        /// </summary>
        public abstract string Name { get; }

        /// <summary>
        /// A collection of category names (categories) that belong to the area
        /// </summary>
        public abstract string[] Categories { get; }

        /// <summary>
        /// Add one or more categories to the area
        /// </summary>
        /// <param name="categories">A collection of category names to add onto the area</param>
        public Area AddCategories(params string[] categories)
        {
            foreach (var category in categories)
            {
                this.Category(category);
            }

            return this;
        }

        /// <summary>
        /// Add a category to the area
        /// </summary>
        /// <param name="name">The name of the category</param>
        public Area Category(string name)
        {
            if (this.Categories
                        .Any(category => category == name) == false)
            {
                Category category = new Category { Name = name };
                OnAddingCategory(this, new EventArgs<Category>(category));
            }

            return this;
        }

        public override string ToString()
        {
            return this.Name;
        }

    }
}
